/*
 * @Description: 路由处理方法
 * @Version: 1.0
 * @Autor: chen_yt
 * @Date: 2022-08-25 13:55:25
 * @LastEditors: chen_yt
 * @LastEditTime: 2022-09-06 15:55:24
 */
import { getAppList } from '@/microapp/qiankun'
import { subApp, mainApp, appPathPrefix } from '@/config'
import { evil } from '@/utils/util'
import Vue from 'vue'

/**
 * @description: 处理子应用路由
 *               name：yy => sub_app_${SubAppName}_yy
 *               path以/xx开头的路由添加alias异名/sub_app_xx/XX，以/sub_app_xx开头的路由不处理
 */
export function addAlias(route) {
  if (!subApp) return

  let nodeBasePath = Vue.prototype.$appBasePath

  let nodeBaseName = Vue.prototype.$appName

  //route的name由yy改为sub_app_xx_yy
  if (route.name && !route.name.includes(nodeBaseName)) { route.name = nodeBaseName + '_' + route.name }

  // 路径从/XX 改为/sub_app_xx/XX
  if (route.path.indexOf('/') === 0 && route.path.indexOf(nodeBasePath) != 0) {
    route.alias = nodeBasePath + route.path
    // route.path = nodeBasePath + route.path
  }

  let children = route.children ?? []

  for (let childroute of children) {
    addAlias(childroute)
  }
}

/**
 * @description: 非qiankun状态下加入*通配
 *               主应用额外增加/sub_app_xx/* 路由
 * @param {Array} routes
 * @return {Array} routes
 * @author: chen_yt
 */
export const addWildcard = function (routes) {
  let path = Vue.prototype.$InQianKun ? `${Vue.prototype.$appBasePath}/*` : '*'
  routes.push({ path: path, redirect: '/404', meta: { config: ['invisible'] } })

  if (mainApp) {
    let subApp = getAppList()
    for (let i = 0; i < subApp.length; i++) {
      let path = subApp[i].activeRoute + '/*'
      if (routes.some(item => item.path == path)) {
        continue
      }
      routes.push({ path: path, meta: { config: ['invisible'] } })
    }
  }
  return routes
}


/**
 * @description:  子应用根据节点筛选出自己的路由并处理
 *                树状搜索
 *                并将以/sub_app_xx为根节点下的二级节点提升为一级节点并添加异名              
 * @param {Array} routes
 * @return {Array} newRoutes
 */
export function filterSubRoute(routes = {}) {
  if (!subApp) {
    return routes
  }

  let prefix = Vue.prototype.$appBasePath
  let newRoutes = []

  let filterNode = (route) => {
    let children = route.children ?? []
    if (route.path === prefix) {
      let arr = children.map(item => {
        let path = item.path ?? ""
        if (path.indexOf('/') != 0) {
          path = '/' + path
        }
        return { ...item, path }
      })
      newRoutes = newRoutes.concat(arr)
      return
    }
    if (route.path.includes(prefix)) {
      newRoutes.push(route)
      return
    }
    for (let subRoute of children) {
      filterNode(subRoute)
    }
  }

  for (let route of routes) {
    filterNode(route)
  }

  for (let route of newRoutes) {
    route = addAlias(route)
  }

  return newRoutes
}

/**
 * @description: 将item的component从string改为function,以满足vue-router需要
 *               例：'/Home/index' 变为 （）=>import(`@/modules/Home/index`)
 *               额外的，主应用会删除子应用路由的component，到达不加载子应用的组件效果
 * @param {*} item
 * @return {*}
 * @author: chen_yt
 */
export const importComponent = function (item = {}) {
  if (mainApp) {
    let subPrefix = '/' + appPathPrefix
    if (item.path.includes(subPrefix)) {
      deleteComponent(item)
      return false
    }
  }

  try {
    let uri = item.component
    if (typeof uri == 'function') {
      return false
    }

    if (/^ModuleView/.test(uri)) {
      item.component = evil(uri)
    } else {
      item.component = () => import(`@/modules${uri}`)
    }
  }
  catch (e) { return e }

  if (item.children) {
    for (let i = 0; i < item.children.length; i++) {
      importComponent(item.children[i])
    }
  }
}

const deleteComponent = function (item = {}) {
  if (item.component) {
    delete item.component
  }
  if (item.children) {
    for (let i = 0; i < item.children.length; i++) {
      deleteComponent(item.children[i])
    }
  }
}



